package org.alvin.code.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author liuqc
 * @version 1.0
 */
public class DateUtil {

    /**
     * 日期转字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }


    /**
     * 将"2015-08-31 21:08:06"型字符串转化为Date
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date StringToDate(String str, String pattern) {
        Date date = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            date = (Date) formatter.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据时间加分钟
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date addMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * 根据时间加月份
     *
     * @param date
     * @param month
     * @return
     */
    public static Date addMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    /**
     * 根据时间加年
     *
     * @param date
     * @param year
     * @return
     */
    public static Date addYear(Date date, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, year);
        return calendar.getTime();
    }


    /**
     * 获取日期列表
     *
     * @param dateStr
     * @return
     * @throws Exception
     */
    public static List<String> getDateList(String dateStr) {
        List<String> dates = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date d = StringToDate(dateStr, "yyyyMM");
        Date date = getMonthStart(d);
        Date monthEnd = getMonthEnd(d);
        while (!date.after(monthEnd)) {
            System.out.println(sdf.format(date));
            dates.add(sdf.format(date));
            date = getNext(date);
        }
        return dates;
    }

    /**
     * 根据开始和结束时间，来计算所有中间空缺的时间
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getDateList(Date startTime, Date endTime) {
        endTime = DateUtil.setEnd(endTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        List<String> dates = new ArrayList<String>();
        do {
            dates.add(sdf.format(startTime));
            startTime = setEnd(getNext(startTime));
        } while (startTime.getTime() < endTime.getTime());
        return dates;
    }


    public static Date getMonthStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (1 - index));
        return calendar.getTime();
    }

    public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (-index));
        return calendar.getTime();
    }

    private static Date getNext(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }

    public static Date setStart(Date date) {
        Calendar start = Calendar.getInstance();
        start.setTime(date);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);
        return start.getTime();
    }

    public static Date setEnd(Date date) {
        Calendar start = Calendar.getInstance();
        start.setTime(date);
        start.set(Calendar.HOUR_OF_DAY, 23);
        start.set(Calendar.MINUTE, 59);
        start.set(Calendar.SECOND, 59);
        return start.getTime();
    }


    /**
     * 分钟换算成时分秒格式
     *
     * @param minutes
     * @return
     */
    public static String remainTimeStr(int minutes) {
        int hour = minutes / 60;
        int minute = minutes % 60;
        int seconds = minutes * 60 % 60;
        return String.format("%s时%s分%s秒", hour, minute, seconds);
    }


    /**
     * 两个时间相减得到小时
     *
     * @param dateTime
     * @return
     */
    public static Long subtractHour(Date dateTime) {
        //每小时毫秒数
        long nh = 1000 * 60 * 60;
        if (dateTime == null) {
            return 0L;
        }
        //获取两个时间差毫秒数
        long diff = System.currentTimeMillis() - dateTime.getTime();
        //相差的小时
        Long hour = diff / nh;

        return hour;
    }

    public static List<Date> getMonths(Date startDate, Date endDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        List<Date> resultList = new ArrayList<>();
        resultList.add(startDate);
        while (startDate.before(endDate)) {
            calendar.add(Calendar.MONTH, 1);
            resultList.add(calendar.getTime());
            startDate = calendar.getTime();
        }
        resultList.add(endDate);
        return resultList;
    }

    /**
     * 比较两个时间 时分秒 大小
     *
     * @param s1
     * @param s2
     * @return
     */
    public static boolean compTime(String s1, String s2) {
        try {
            if (s1.indexOf(":") < 0 || s2.indexOf(":") < 0) {
                System.out.println("格式不正确");
            } else {
                String[] array1 = s1.split(":");
                int total1 = Integer.valueOf(array1[0]) * 3600 + Integer.valueOf(array1[1]) * 60 + Integer.valueOf(array1[2]);
                String[] array2 = s2.split(":");
                int total2 = Integer.valueOf(array2[0]) * 3600 + Integer.valueOf(array2[1]) * 60 + Integer.valueOf(array2[2]);
                return total1 - total2 > 0 ? true : false;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 时分秒比较
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean hourByMinuteComp(String startTime, String endTime) {
        GregorianCalendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        String hour_str = String.valueOf(hour);
        String minute_str = String.valueOf(minute);
        String second_str = String.valueOf(second);

        String nowTime = hour_str + ":" + minute_str + ":" + second_str;
        boolean startT = compTime(nowTime, startTime);
        boolean endT = compTime(nowTime, endTime);
        if (startT && !endT) {
            return true;
        }

        return false;
    }

    private static final SimpleDateFormat longDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");

    public static Date parseDataToLongFormat(String date) throws ParseException {
        return longDateFormat.parse(date);
    }
 

}
